$(function(){

	var login = {
		keydown: function() {
			$(document).keydown(function(e){
				var curKey = e.which;
				if(curKey == 13){
					// $("#回车事件按钮控件").click();
					$('[data-role="login"]').trigger('click');
					return false;
				}
			});
		},
		sendData: function() {
			$('[data-role="login"]').on('click', function() {
				var name = $('[data-role="loginname"]').val();
				var password = $('[data-role="loginpass"]').val();
				if(name == '' || password == '') {
					iosOverlay({
						text: "账号或者密码不能为空!",
						duration: 1500
					});
				} else {
					$.ajax({
					    url: wsCommon.host + '/api/member/login',
					    dataType: wsCommon.jsonType,
						data: {
							loginAccount: encodeURIComponent(name).replace(/%E2%80%AC/, ''),
							loginPassword: password,
							remember: $('#for_remember').is(':checked')
						},
					    success: function(res) {
							if(res.code == 1) {
								if(res.result) {
									wsCommon.setCookie('uid', res.data.user.memberId);
									iosOverlay({
										text: '登录成功，正在跳转页面',
										duration: 1000,
										onbeforehide: function() {
											var link = wsCommon.getCookie('go_back_link');
											if(link && link.indexOf('cart2') != -1) {
												link = './cart1.html?source=login';
											} else if(link && link.indexOf('lost') != -1) {
												link = './index.html?source=login';
											} else {
												if(link.indexOf('?') != -1) {
													link += '&source=login'
												} else {
													link += '?source=login'
												}
											}
											window.location.href = link ? link : './index.html?source=login';
										}
									});
								} else {
									iosOverlay({
										text: res.reason,
										duration: 1500
									});
								}
							} else {
								iosOverlay({
									text: res.reason,
									duration: 1500
								});
							}
					    },
					    error: function() {
					        // console.log('登录显示接口返回失败');
					    }
					})
				}

			})
		},
		init: function() {
			this.sendData();
			this.keydown();
		}
	}

	login.init();
})
